﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace HurricanePackage
{
    public class JsonTools
    {

        /// <summary>
        /// 根据路劲加载文本
        /// </summary>
        /// <param name="_paths"></param>
        /// <returns></returns>
        public static string LoadTextFromPath(params string[] _paths)
        {
            if (_paths == null)
                return "";
            string str = "";
            string savePath = CombineJsonPath(_paths);
            string directoryName = Path.GetDirectoryName(savePath);
            //路径不存在则创建
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(directoryName);
                str = File.ReadAllText(CombineJsonPath(_paths));
            }
            else
            {
                str = File.ReadAllText(CombineJsonPath(_paths));
            }
            return str;
        }

        /// <summary>
        /// 合并路径
        /// </summary>
        /// <param name="_paths"></param>
        /// <returns></returns>
        public static string CombineJsonPath(params string[] _paths)
        {
            string pathStr = "";
            for (int i = 0; i < _paths.Length; i++)
            {
                pathStr = Path.Combine(pathStr, _paths[i]);
            }
            return pathStr;
        }
        /// <summary>
        /// json字符串转对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_jsonStr"></param>
        /// <returns></returns>
        public static T Json2Obj<T>(string _jsonStr)
        {
            return JsonUtility.FromJson<T>(_jsonStr);
        }
        /// <summary>
        /// 对象转json字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_target"></param>
        /// <returns></returns>
        public static string Obj2Json<T>(T _target)
        {
            return JsonUtility.ToJson(_target,true);
        }
        /// <summary>
        /// 存储文件到本地
        /// </summary>
        /// <param name="_target"></param>
        /// <param name="_paths"></param>
        public static void SaveTextToFile(string _target, params string[] _paths)
        {
            string savePath = CombineJsonPath(_paths);
            string directoryName = Path.GetDirectoryName(savePath);
            //路径不存在则创建
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(directoryName);
                File.WriteAllText(savePath,_target);
            }
            else
            {
                File.WriteAllText(savePath, _target);
            }
        }
        /// <summary>
        /// 保存数据为json格式到本地
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_obj"></param>
        /// <param name="_paths"></param>
        public static void SaveObjAsJsonStr<T>(T _obj, params string[] _paths)
        {
            string str = Obj2Json(_obj);
            SaveTextToFile(str,_paths);
        }
        /// <summary>
        /// 根据路径加载json文件为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_paths"></param>
        /// <returns></returns>
        public static T LoadJsonFileAsObj<T>(params string[] _paths)
        {
            string jsonStr = LoadTextFromPath(_paths);
            return Json2Obj<T>(jsonStr);
        }

    }
}
